package com.cloudlead.common.worker.action.impl;

import com.cloudlead.common.lang.BusinessModule;
import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.lang.service.CrudService;
import com.cloudlead.common.worker.action.CrudActionFactory;

import java.util.List;

public abstract class AbstractCrudActionFactory<Service extends CrudService<VO>, VO extends IdEntity<String>>
        extends AbstractActionFactory implements CrudActionFactory<VO> {

    protected final Service service;

    public AbstractCrudActionFactory(BusinessModule module, Service service) {
        super(module);
        this.service = service;
    }

    @Override
    public VO create(VO vo) {
        service.create(vo);
        return vo;
    }

    @Override
    public VO update(VO vo) {
        service.update(vo);
        return vo;
    }

    @Override
    public void updateAll(VO vo, Filter filter) {
        service.updateAll(vo, filter);
    }

    @Override
    public void updateAllByExample(VO vo, VO example) {
        service.updateAll(vo, example);
    }

    @Override
    public VO view(String id) {
        return service.view(id);
    }

    @Override
    public long count(Filter filter) {
        return service.count(filter);
    }

    @Override
    public long countByExample(VO example) {
        return service.count(example);
    }

    @Override
    public List<VO> listByExample(VO example) {
        return service.list(example, null);
    }

    @Override
    public VO findOne(Filter filter) {
        return service.findOne(filter);
    }

    @Override
    public VO findOneByExample(VO example) {
        return service.findOne(example);
    }

    @Override
    public List<VO> list(Filter filter, Sort sort) {
        return service.list(filter, sort);
    }

    @Override
    public Page<VO> findAll(PageRequest pageable) {
        return service.findAll(pageable);
    }

    @Override
    public VO delete(String id) {
        VO view = view(id);
        service.delete(id);
        return view;
    }

    @Override
    public void deleteAll(Filter filter) {
        service.deleteAll(filter);
    }

    @Override
    public void deleteAllByExample(VO example) {
        service.deleteAll(example);
    }

}
